from django.db import models
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse


class Country(models.Model):
    created = models.DateTimeField(auto_now_add=True, editable=False)
    updated = models.DateTimeField(auto_now=True, editable=False)

    name = models.CharField('denumire', max_length=20, unique=True,
        help_text='denumirea tarii')

    def __unicode__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('country_detailview', kwargs={'pk': self.pk})

    class Meta:
        verbose_name = 'country'
        verbose_name_plural = 'countries'


class County(models.Model):
    created = models.DateTimeField(auto_now_add=True, editable=False)
    updated = models.DateTimeField(auto_now=True, editable=False)

    name = models.CharField('denumire', max_length=20, unique=True,
        help_text='denumirea judetului')
    country = models.ForeignKey('country', verbose_name='tara',
        help_text='tara din care face parte judetul')

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = 'judet'
        verbose_name_plural = 'judete'
        unique_together = (('name', 'country'),)


class Address(models.Model):
    created = models.DateTimeField(auto_now_add=True, editable=False)
    updated = models.DateTimeField(auto_now=True, editable=False)

    address = models.TextField('adresa', max_length=100, help_text='Adresa de strada')
    county = models.ForeignKey(County, verbose_name=u'Judet')

    def __unicode__(self):
        return "County: %s, Country: %s, Address: %s" % (
            self.county,
            self.county.country,
            self.address)

    class Meta:
        verbose_name = 'adresa'
        verbose_name_plural = 'adrese'
        unique_together = (('address', 'county'),)


class Partner(models.Model):
    created = models.DateTimeField(auto_now_add=True, editable=False)
    updated = models.DateTimeField(auto_now=True, editable=False)
    created_by = models.ForeignKey(User, editable=False)

    name = models.CharField('Denumire', max_length=50, unique=True, help_text='Denumirea partenerului persoana juridica sau fizica')
    address = models.ForeignKey(Address, verbose_name='Adresa')

    fiscal_code = models.DecimalField('CIF', max_digits=10, decimal_places=0, unique=True, help_text='Cod de identificare fiscala (CIF/CUI)')
    fiscal_attr = models.CharField('Atribut fiscal', max_length=2, blank=True, null=True, help_text='Categoria de platitor de taxe si impozite la bugetul de stat')

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = 'partener'
        verbose_name_plural = 'parteneri'
        unique_together = (('name', 'address', 'fiscal_code'),)


class Workplace(models.Model):
    created = models.DateTimeField(auto_now_add=True, editable=False)
    updated = models.DateTimeField(auto_now=True, editable=False)
    created_by = models.ForeignKey(User, editable=False)

    name = models.CharField('denumire', max_length=20, unique=True,
        help_text='denumirea punctului de lucru')

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = 'punct de lucru'
        verbose_name_plural = 'puncte de lucru'


class Vat(models.Model):
    created = models.DateTimeField(auto_now_add=True, editable=False)
    updated = models.DateTimeField(auto_now=True, editable=False)

    value = models.DecimalField('valoare', max_digits=2, decimal_places=0,
        unique=True,
        help_text='cota procentuala a taxei pe valoare adaugata')

    def __unicode__(self):
        return "%d %%" % self.value

    class Meta:
        verbose_name = 'cota tva'
        verbose_name_plural = 'cote tva'


class DocumentType(models.Model):
    created = models.DateTimeField(auto_now_add=True, editable=False)
    updated = models.DateTimeField(auto_now=True, editable=False)

    code = models.CharField('cod', max_length=5,
        help_text='codul tipului de document')
    name = models.CharField('denumire', max_length=50,
        help_text='numele descriptiv al tipului de document')
    description = models.TextField('descriere', blank=True,
        help_text='descriere optionala a tipului de document, destinatia de utilizare, etc.')


class Document(models.Model):
    # Generic columns
    created = models.DateTimeField(auto_now_add=True, editable=False)
    updated = models.DateTimeField(auto_now=True, editable=False)
    user = models.ForeignKey(User, editable=False)
    workplace = models.ForeignKey(Workplace, editable=False)
    # Specific columns
    document_type = models.ForeignKey(DocumentType, editable=False,
        help_text='tipul documentului')
    issued_date = models.DateField('emis la', help_text='data emiterii')
    due_date = models.DateField('scadent la', empty=True, null=True,
        help_text='data scadentei')
    # TODO there are two links to Partner so they need to be named specifically
    # in an attribute
    issued_by = models.ForeignKey(Partner, verbose_name='emis de',
        related_name='issuedby',  # produces self.issuedby_set db Manager
        help_text='partenerul care a emis documentul')
    issued_to = models.ForeignKey(Partner, verbose_name='emis catre',
        related_name='issuedto',  # produces self.issuedto_set db Manager
        help_text='partenerul catre care s-a emis documentul')
    series = models.CharField('serie', max_length=10,
        help_text='seria documentului')
    number = models.DecimalField('numar', max_digits=10, decimal_places=0,
        help_text='numarul documentului')
    payed = models.BooleanField('documentul a fost platit?',
        help_text='a fost emisa chitanta (catre cumparator) sau a fost primita chitanta (de la emitent)')
    notes = models.TextField('notite', blank=True,
        help_text='notite optionale legate de document')
